<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
 * Description of category_model
 *
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Admin_listing_model extends CI_Model {

	//put your code here


	function __construct() {
		parent::__construct();
		log_message('debug', 'Init Model Initialized');
	}

	//-- Get listings(Multi Row) --//
	function get_listings($status = NULL) {
		
		$sql = "select * from lawyer_master where status = $status order by id DESC";	
		$query = $this->db->query($sql);
		$result = $query->result();
		if($result){
			return $query->result();
		}else{
			return false;
		}

	}
	
	/*For PAGINATION*/
	/*function get_listings($featured=false, $status=1,$num=NULL, $offset=NULL) {
		$subcond = "where status=$status";
		$limit = "";
		if (isset($num) && isset($offset))
			$limit = " order by (id) desc limit $offset, $num";
		$sql = "select * from business_master " . $subcond . $limit;
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}*/
	//--Inactive listing function for pagination-- rahul sinha//
	function get_inactive_listings($featured=false, $status=51,$num=NULL, $offset=NULL) {
		$subcond = "where status=$status";
		$limit = "";
		if (isset($num) && isset($offset))
			$limit = " order by (id) desc limit $offset, $num";
		$sql = "select * from lawyer_master " . $subcond . $limit;
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}
	
	function search($search=null){
		$sql = "Select * from lawyer_master where title like '$search%' order by id desc";
		$query = $this->db->query($sql);
		$result = $query->result();
		if($result){
			return $query->result();
		}else{
			return false;
		}


	}

	function change_status($id, $status) {
		if ($status == 1) {
			$sql = "update lawyer_master set status=51 where id=$id";
		}
		if ($status == 51) {
			$sql = "update lawyer_master set status=1 where id=$id";
		}
		$this->db->query($sql);
	}
	/*
	 function change_status($status=NUll, $id=NULL)
	 {

	$sql="update business_master set status=$status where id=$id";
	$this->db->query($sql);
	}
	*/
	
	
	function delete($id=NULL)
	{
		$this->db->where('id',$id);
		$this->db->delete('lawyer_master');

		$this->db->where('listing_id', $id);
		$this->db->delete('review_master');
		
		$this->db->where('listing_id', $id);
		$this->db->delete('photos');
		
		$this->db->where('listing_id', $id);
		$this->db->delete('videos');
		
		$this->db->where('listing_id', $id);
		$this->db->delete('products');
		
		$this->db->where('listing_id', $id);
		$this->db->delete('deal_master');
		//$sql="delete from business_master where id=$id";
		//$this->db->query($sql);
		
		
	}

	function get_business_listing($id=NULL) {
		$sql = "select * from lawyer_master where id=$id";
		$query = $this->db->query($sql);
		$row = $query->row();
		return $row;
	}

	function get_listing_keyword($listing_id=0,$key_id=0){
		$sql = "select * from listing_keyword where listing_id=$listing_id and key_id=$key_id limit 1";
		$query = $this->db->query($sql);
		if($query->num_rows()!=0){
			return $query->row()->key_id;
		}else{
			return 0;
		}

			
			
	}

	function get_business_name($id= NULL)
	{
		$sql = "select * from profile where id= $id";
		$query = $this->db->query($sql);
		$row = $query->row();
		return $row;
			
	}

	function get_members($status = 51) {
		if (!$this->db->table_exists('member_master')) {
			return FALSE;
		}
		$sql = "select * from member_master where status=$status order by id";
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}

	function get_photos($id=NULL){
		$sql = "Select * from photos where listing_id=$id and status=51 order by id desc";
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}
	public function listing_logo($listing_id) {
		$sql = "select * from lawyer_master where id='$listing_id' ";
		$query = $this->db->query($sql);
		$row = $query->row();
		return $row;
	}

	function get_workinghours($id=NULL) {
	
		$sql = "Select *from working_hours where listing_id=$id and status=51 order by id desc limit 1";
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}
	function edit_working_hours($id=NULL){
		 
		$sql = "Select *from working_hours where id=$id";
		$query = $this->db->query($sql);
		$row = $query->row();
		return $row;
	}
	function delete_working_hours($id=NULL)
	{
		$sql="delete from working_hours where id=$id";
		$this->db->query($sql);
	}
	
	function get_videos($id=NULL){
		$sql = "Select * from videos where listing_id=$id and status=51 order by id desc";
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}
	function get_products($id=NULL)
	{
		$sql = "Select *from products where listing_id=$id and status=51 order by id desc";
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}
	
	function product_edit($listing_id=0)
	{
		$sql = "select * from products where id=$listing_id";
		//print $sql;
		//die();
		 
		$query = $this->db->query($sql);
		$row = $query->row();
		return $row;
	}
	function delete_product($id,$image_file) {
		if($id && $image_file){
			$sql="delete from products where id=$id";
	
			$this->db->query($sql);
			//--Delete Image File --//
	
			$f = BASEPATH.'../assets/images/data/product/'.$image_file;
			//$f = base_url()."assets/images/data/advertisement/".$image_file;
	
			unlink($f);
		}
	}
	function get_deal($id=NULL) {
		 
		$sql = "Select *from deal_master where listing_id=$id and status=51 order by id desc limit 1";
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}
	
	function deal_edit($id=NULL) {
	
		$sql = "Select *from deal_master where id=$id";
		$query = $this->db->query($sql);
		$row = $query->row();
		return $row;
	}
	
	function delete_deal($id,$image_file) {
		if($id && $image_file){
			$sql="delete from deal_master where id=$id";
	
			$this->db->query($sql);
			//--Delete Image File --//
	
			$f = BASEPATH.'../assets/images/data/deal/'.$image_file;
			//$f = base_url()."assets/images/data/advertisement/".$image_file;
	
			unlink($f);
		}
	}
}